package com.example.jf.jfapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.activity.ImagePagerActivity;
import com.example.jf.jfapp.bean.response.DongTaiSquareResponse;
import com.example.jf.jfapp.widget.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by jf on 2017/7/28.
 */

public class SquareAdapter extends RecyclerView.Adapter {
    List<DongTaiSquareResponse.ResultBean> dLists;
    private ActionClickListener mClickListener;
    boolean praise=false;

    public void setmClickListener(ActionClickListener mClickListener) {
        this.mClickListener = mClickListener;
    }
    private Context mContext;
    public SquareAdapter(List<DongTaiSquareResponse.ResultBean> dLists, Context context) {
        this.dLists = dLists;
        this.mContext = context;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_square_dongtai, parent, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }



    class ItemViewHolder extends RecyclerView.ViewHolder {
        CircleImageView usertou;
        TextView username;
        TextView usercontent;
        RecyclerView rv;
        ImageView usercomment;
        TextView usercommentSize;
        ImageView userlove;
        ImageView contentImage;
        TextView userloveSize;

        public ItemViewHolder(View itemView) {
            super(itemView);
            usertou = (CircleImageView) itemView.findViewById(R.id.user_touxiang);
            username = (TextView) itemView.findViewById(R.id.user_name);
            usercontent = (TextView) itemView.findViewById(R.id.user_content);
            contentImage = (ImageView) itemView.findViewById(R.id.tv_publishpicture);
            rv = (RecyclerView) itemView.findViewById(R.id.rv);
            usercomment = (ImageView) itemView.findViewById(R.id.user_comment);
            usercommentSize = (TextView) itemView.findViewById(R.id.user_commentSize);
            userlove = (ImageView) itemView.findViewById(R.id.user_love);
            userloveSize = (TextView) itemView.findViewById(R.id.user_loveSize);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        final DongTaiSquareResponse.ResultBean request = dLists.get(position);
        Glide.with(itemViewHolder.itemView.getContext())
                .load(request.getAuthor().getHeadProtrait())
                .transform(new GlideCircleTransform(itemViewHolder.itemView.getContext()))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(itemViewHolder.usertou);

        itemViewHolder.username.setText(request.getAuthor().getUserName());
        itemViewHolder.usercontent.setText(request.getMessage());
        if (request.isPraiseStatus()) {
            itemViewHolder.userlove.setImageResource(R.drawable.ic_love);

        } else {

            itemViewHolder.userlove.setImageResource(R.drawable.ic_love_click1);

        }


        for (int i = 0; i < request.getImageUrls().size(); i++) {
            String imageUrl = request.getImageUrls().get(i);
            List<String> images = request.getImageUrls();
            if (images != null && !images.isEmpty()) {
                int spanCount = 3;
                if (images.size() == 4) {
                    spanCount = 2;
                }
                //使用adapter显示
                itemViewHolder.rv.setLayoutManager(new GridLayoutManager(
                        itemViewHolder.itemView.getContext(), spanCount)
                );
                StatuImageAdapter statuImageAdapter = new StatuImageAdapter(images);
                itemViewHolder.rv.setAdapter(statuImageAdapter);
                itemViewHolder.rv.setVisibility(View.VISIBLE);
                itemViewHolder.contentImage.setVisibility(View.GONE);

            } else {
                itemViewHolder.rv.setVisibility(View.GONE);
                itemViewHolder.contentImage.setVisibility(View.VISIBLE);

                if (imageUrl != null && !"".equals(imageUrl)) {
                    if (imageUrl.endsWith(".gif")) {
                        Glide.with(mContext)
                                .load(imageUrl)
                                .asGif()
                                .error(R.mipmap.ic_error)
                                .dontAnimate()
                                .centerCrop()
                                .diskCacheStrategy(DiskCacheStrategy.SOURCE)// DiskCacheStrategy.NONE
                                .into(itemViewHolder.contentImage);
                    } else {
                        Glide.with(mContext)
                                .load(imageUrl)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .crossFade()
                                .error(R.mipmap.ic_error)
                                .centerCrop()
                                .into(itemViewHolder.contentImage);
                    }
                }
            }
            Glide.with(mContext)
                    .load(request.getImageUrls().get(i))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .fitCenter()
                    .into(itemViewHolder.contentImage);

            itemViewHolder.usercommentSize.setText(request.getCommentsSize() + "");
            itemViewHolder.userloveSize.setText(request.getPraiseSize() + "");
            //itemViewHolder.userlove.setImageResource(R.drawable.ic_love_click1);

            //itemViewHolder.usercommentSize.setText(request.getCommentsSize());
            //itemViewHolder.usercomment.setImageResource(R.drawable.ic_laugh);
            //itemViewHolder.userlove.setImageResource(R.drawable.ic_love);
            // itemViewHolder.userloveSize.setText(request.getPraiseSize());
            //适配多张图，无须理会
            final ArrayList<String> url = new ArrayList<>();
            url.add(request.getImageUrls().get(i));


            itemViewHolder.contentImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext, ImagePagerActivity.class);
                    intent.putExtra("image_index", 0);
                    intent.putStringArrayListExtra("image_urls", url);
                    mContext.startActivity(intent);
                }
            });
            itemViewHolder.usercomment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickListener != null) {
                        mClickListener.commentClick(request, position);
                    }
                }
            });
            itemViewHolder.userlove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickListener != null) {
                        mClickListener.pariseClick(request, position);
                   if (itemViewHolder.userlove.isPressed()){
                       itemViewHolder.userlove.setImageResource(R.drawable.ic_love_click1);
                       praise=true;
                   }
                   else{
                       itemViewHolder.userlove.setImageResource(R.drawable.ic_love);
                       praise=false;
                   }
                    }
                }
            });

            itemViewHolder.usertou.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickListener != null) {
                        mClickListener.headClick(request, position);
                    }
                }
            });
            itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickListener != null) {
                        mClickListener.contentClick(request, position);
                    }
                }
            });
        }

    }


@Override
    public int getItemCount(){
        return dLists.size();
    }
    //点击事件的回调
    public interface ActionClickListener {
        void commentClick(DongTaiSquareResponse.ResultBean resultBean, int position);

        void pariseClick(DongTaiSquareResponse.ResultBean resultBean, int position);

        //头像
        void headClick(DongTaiSquareResponse.ResultBean resultBean, int position);

        //内容进入详情
        void contentClick(DongTaiSquareResponse.ResultBean resultBean, int position);
    }
}
